<template>
    <div>

      <el-dialog
        title="新增文档"
        :visible.sync="dialogVisible"
        width="500px"
        append-to-body
        :before-close="handleClose">

        <el-form ref="documentForm" :model="datas" :rules="rules" class="demo-form-inline" label-width="150px" size="medium" label-position="left">
          <el-form-item label="文档名称" prop="file_name">
            <el-input v-model="datas.file_name" style="width: 100%" placeholder="请输入文档名称" clearable></el-input>
          </el-form-item>
          <el-form-item label="文档交付所属任务" prop="task_id" v-if="!isTask">
            <el-select v-model="datas.task_id" @change="filePlanChange" placeholder="请选择所属任务" style="width: 100%" clearable>
              <el-option
                v-for="item in plans"
                :key="item.id"
                :label="item.task_name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="文档类型" prop="file_type">
            <el-select v-model="datas.file_type" @change="typeChange" placeholder="请选择文档类型" style="width: 100%" clearable>
              <el-option
                v-for="(item,index) in options"
                :key="index"
                :label="item.dict_val"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="申报所需文件" prop="delivery">
            <el-switch v-model="datas.need"></el-switch>
          </el-form-item>

        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="filesConfirm">确 定</el-button>
        </span>
      </el-dialog>

    </div>
</template>

<script>
    export default {
        name: "documentEdit",
        props:{
            datas:{
                type:Object,
                default:function () {
                    return {}
                }
            },
            plans:{
                type:Array,
                default:function () {
                    return [];
                }
            },
            isTask:{
                type:Boolean,
                default:false
            }
        },
        data:function () {
            return{
                dialogVisible:false,
                options:[],
                rules: {
                    file_name: [
                        { required: true, message: '请输入文档名称', trigger: 'blur' },
                    ],
                    task_id: [
                        { required: true, message: '请选择所属任务', trigger: 'change' }
                    ],
                    file_type: [
                        { required: true, message: '请选择文档类型', trigger: 'change' }
                    ],
                }
            }
        },
        mounted:function(){
            this.options = this.$store.getters.sysDic('file_type')
        },
        methods:{
            openDialog:function () {
                this.dialogVisible = true;
            },
            filesConfirm:function () {
                if (this.$refs.documentForm){
                    this.$refs.documentForm.validate((val)=>{
                        if (val){
                            this.$emit("succeed")
                            this.dialogVisible = false
                        }
                    })
                }else {
                    this.dialogVisible = false
                }
            },
            handleClose:function () {
                this.dialogVisible = false;
            },
            filePlanChange:function (id) {
                let index = this.plans.findIndex(a=>a.id == id)
                if (index != -1){
                    let plan = this.plans[index];
                    this.datas.task_id = plan.id + "";
                    this.datas.task_name = plan.task_name;
                }
            },
            typeChange:function (id) {
                let index = this.options.findIndex(a=>a.id == id)
                if (index != -1){
                    let data = this.options[index];
                    this.datas.file_type = data.id
                    this.datas.type_name = data.dict_val
                }
            },
        }
    }
</script>

<style scoped>

</style>
